<template>
	<view id="main">
		<view id="content">
		<view class="item">
			<text>联系人</text><input class="input-adrs" placeholder="姓名" v-model="contractperson" maxlength="20"/>
		</view>
		<view class="br"></view>
		<view class="item">
			<text>电话</text><input class="input-adrs" placeholder="电话" v-model="contractphone" maxlength="11"/>
		</view>
		<view class="br"></view>
		<view class="item">
			<text>地址</text><input class="input-adrs" placeholder="输入收货地址" v-model="address" maxlength="20"/>
		</view>
		<view class="br"></view>
		<view class="item">
			<text>门牌号</text><input class="input-adrs" placeholder="例:5号楼203室" v-model="detailaddress"  maxlength="20"/>
		</view>
		
		
		<button  class="confirm" @click="save()">保存</button>
		</view>
		
	</view>
</template>

<script>
	export default{
		data(){
			return{
				aid:"",
				contractperson:"",
				contractphone:"",
				address:"",
				detailaddress:"",
				flag:"",
			}
		},
		methods:{
			save(){
				var phoneReg = /^1[34578]\d{9}$/;
				if(this.contractperson.length==0){
					uni.showToast({
						title:"请输入联系人",
						icon:"none"
					})
				}
				else if(!phoneReg.test(this.contractphone)){
					uni.showToast({
						title:"请输入正确的电话",
						icon:"none"
					})
				}
				else if(this.address.length==0){
					uni.showToast({
						title:"请输入地址",
						icon:"none"
					})
				}
				else if(this.detailaddress.length==0){
					uni.showToast({
						title:"请输入门牌号",
						icon:"none"
					})
				}
				
				else{
					
					
					uni.request({
						url:getApp().globalData.http+"/user/modifyAddress",
						method:"POST",
						header:{
							'content-type':"application/x-www-form-urlencoded",
							"Authorization":uni.getStorageSync("token")
						},
						data:{
							addressid:this.aid,
							contractperson:this.contractperson,
							contractphone:this.contractphone,
							address:this.address,
							detailaddress:this.detailaddress,
						},
						success: (res) => {
							switch(res.data){
								case "success":{
									if(this.flag=="modify"){
										uni.navigateBack({
											delta:getCurrentPages().length-3
										})
									}
									else{
										uni.navigateBack({
											delta:getCurrentPages().length-5
										})
									}
									
									uni.showToast({
										title:this.flag,
										icon:"success"
									})
									break;
								}
								case "no_token":{
									uni.showToast({
										title:"没有权限"
									})
									break;
								}
								case "fail":{
									uni.showToast({
										title:"失败，请稍后重试"
									})
									break;
								}
							}
						}
					})
				}
				
			}
		},
		onLoad:function(option){
			this.aid=option.aid
			this.address=option.abig
			this.detailaddress=option.adetail
			this.contractperson=option.conper
			this.contractphone=option.phone
			this.flag=option.flag
		},
		onNavigationBarButtonTap() {
			uni.request({
				url:getApp().globalData.http+"/user/deleteAddress",
				method:"POST",
				header:{
					'content-type':"application/x-www-form-urlencoded",
					"Authorization":uni.getStorageSync("token")
				},
				data:{
					addressid:this.aid
				},
				success: (res) => {
					if(res.data=="success"){
						var addressidInStorage=uni.getStorageSync("addressid");
						if(addressidInStorage==this.aid){
							uni.removeStorageSync("addressid");
							uni.removeStorageSync("address");
							uni.removeStorageSync("detailaddress");
							uni.removeStorageSync("person");
							uni.removeStorageSync("phone");
						}
						uni.navigateBack({
							delta:getCurrentPages().length-3
						});
						uni.showToast({
							title:"已删除",
							duration:1000
						});
					}
					else if(res.data=="fail"){
						uni.showToast({
							title:"请稍后再试",
							icon:"none",
							duration:2000
						});
					}
					else{
						uni.showToast({
							title:"没有权限",
							icon:"none",
							duration:2000
						});
					}
					
				}
				
			})
		}
	}
	
</script>

<style scoped>
	#main{
		height: 100%;
		background-color: #F4F4F4;
		overflow: hidden;
	}
	.item{
		height: 60px;
		/* line-height: 20px; */
		display: flex;
		flex-direction: row;
		background-color: #FFFFFF;
		align-content: center;
		
		
	}
	.input-adrs{
		width: 80%;
		padding-bottom: 5px;
		
		margin-left: 10px;
		margin-top: 20px;
		color: #808080
	}
	
	.confirm{
		background-color: #007AFF;
		border-radius: 10px;
		margin-left: 20px;
		margin-right: 20px;
		margin-top: 30px;
		color: #FFFFFF;
	}
	
	text{
		width: 60px;
		margin-left: 20px;
		margin-top: 20px;
	}
	.br{
		width: 100%;
		height: 0.5px;
		background-color: #C0C0C0;
	}
	#content{
		margin-top: 10px;
		overflow: hidden;
	}
	
	
</style>
